﻿using System.Linq;
using System.Text;
using SharePointOptimizer.Model;
using SharePointOptimizer.BL.DataAnalysis;
using System;
using AG.Utilities;

namespace SharePointOptimizer.BL.DataAnalysis.IssueTypes
{
    public class BlobCachingEnabledCheck : IssueTypeCheckBase
    {

        public BlobCachingEnabledCheck(int issueTypeId)
            : base(issueTypeId)
        {

        }

        public override void Check()
        {

            try
            {
                // Get all Service instance that has ServiceTypeID 1
                Farm farm = dc.Farms.FirstOrDefault();


                if (farm != null)
                {

                    Service service = farm.Services.SingleOrDefault(s => s.TypeName == "Microsoft SharePoint Foundation Web Application");


                    if (service != null)
                    {

                        var QryLists = from w in dc.WebApplications
                                       from s in dc.ConfigSettings
                                       where w.WebApplicationID == s.WebApplicationID
                                       where s.BlobCacheEnabled == false
                                       select new { name = w.DisplayName, id = w.WebApplicationID };
                        foreach (var webApp in QryLists)
                        {
                            Issue issue = new Issue();
                            issue.IssueType = issueType;
                            issue.TargetId = webApp.id;
                            issue.Description = issueType.Description.Replace("{webAppName}", webApp.name);
                            dc.Issues.InsertOnSubmit(issue);

                        }
                        dc.SubmitChanges();
                    }


                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }

}
